package com.s2.m.btchat;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 * Shows several information about the current device
 * @author Samik Saha
 */
public class DeviceStatus implements CommandListener {

    /**
     * Keep the parent reference
     */
    private BTChat parent = null;
    /**
     * A form to show the properties
     */
    private Form form = new Form(" Device Status");
    /**
     * keep a local device instance
     */
    private LocalDevice localDevice = null;
    /**
     * Item to display the status of Chat Setvice
     */
    private StringItem serviceStatus = new StringItem("Chat Service", " Off");
    /**
     * Item to display the device name
     */
    private StringItem deviceNameItem = new StringItem("Device Name", "");
    /**
     * Item to display the user name
     */
    private StringItem deviceAddressItem = new StringItem("Device Addr", "");
    /**
     * Item to display visiblity mode
     */
    private StringItem visiblityItem = new StringItem("Device Visiblity", "");
    /**
     * Local Blutooth Device name
     */
    private String deviceName = "Unknown";
    /**
     * Bluetooth Address of this device
     */
    private String deviceAddress;
    /**
     * bluetooth device visiblity mode
     */
    private int visiblityMode;
    /**
     * Back Command
     */
    private Command backCommand = new Command("Back", Command.CANCEL, 0);

    public DeviceStatus(BTChat parent) {
        this.parent = parent;

        form.append(deviceNameItem);
        form.append(deviceAddressItem);
        form.append(visiblityItem);
        form.append(serviceStatus);

        form.addCommand(backCommand);
        form.setCommandListener(this);
    }

    public void show() {
        try {
            localDevice = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException ex) {
            ex.printStackTrace();
            parent.showException(ex);
        }
        deviceName = localDevice.getFriendlyName();
        deviceNameItem.setText(deviceName);

        deviceAddress = localDevice.getBluetoothAddress();
        deviceAddressItem.setText(deviceAddress);
        visiblityMode = localDevice.getDiscoverable();
        switch (visiblityMode) {
            case DiscoveryAgent.GIAC:
                visiblityItem.setText("Shown to All");
                break;
            case DiscoveryAgent.LIAC:
                visiblityItem.setText("Limited Visiblity");
                break;
            case DiscoveryAgent.NOT_DISCOVERABLE:
                visiblityItem.setText("Not Discoverable");
                break;
            default:
                visiblityItem.setText("Unknown");
                break;
        }

        serviceStatus.setText(Server.isRunning() ? "On" : "Off");

        Display.getDisplay(parent).setCurrent(form);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == backCommand) {
            parent.showUI();
        }
    }
}
